<template>
  <el-dialog :title="!dataForm.commentId ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="80px">
      <el-form-item label="评价用户ID" prop="userId">
        <el-input v-model="dataForm.userId" placeholder="评价用户ID"></el-input>
      </el-form-item>
      <el-form-item label="商品ID" prop="productId">
        <!-- <el-input v-model="dataForm.productId" placeholder="商品ID"></el-input> -->
        <ProductAutocomplete v-model="dataForm.productId" placeholder="商品ID"></ProductAutocomplete>
      </el-form-item>
      <el-form-item label="关联订单号" prop="orderId">
        <el-input v-model="dataForm.orderId" placeholder="关联订单号"></el-input>
      </el-form-item>
      <el-form-item label="评价内容（500字内）" prop="content">
        <el-input v-model="dataForm.content" placeholder="评价内容（500字内）"></el-input>
      </el-form-item>
      <el-form-item label="星级评分（1-5）" prop="rating">
        <!-- <el-input v-model="dataForm.rating" placeholder="星级评分（1-5）"></el-input> -->
        <el-rate v-model="dataForm.rating" show-text>
        </el-rate>
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <!--   <el-input v-model="dataForm.status" placeholder="审核状态"></el-input> -->
        <el-select v-model="dataForm.status" placeholder="请选择">
          <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="评价时间" prop="createdAt">
        <!-- <el-input v-model="dataForm.createdAt" placeholder="评价时间"></el-input> -->
        <el-date-picker v-model="dataForm.createdAt" type="datetime" value-format="YYYY-MM-DD HH:mm:ss"
          placeholder="选择日期时间">

        </el-date-picker>

      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>

import ProductAutocomplete from "@/views/modules/common/product-autocomplete"
export default {
  components: {
    ProductAutocomplete
  },
  data() {
    return {
      visible: false,
      dataForm: {
        commentId: 0,
        userId: '',
        productId: '',
        orderId: '',
        content: '',
        rating: '',
        status: 0,
        createdAt: ''
      },
      dataRule: {
        userId: [
          { required: true, message: '评价用户ID不能为空', trigger: 'blur' }
        ],
        productId: [
          { required: true, message: '商品ID不能为空', trigger: 'blur' }
        ],
        orderId: [
          { required: true, message: '关联订单号不能为空', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '评价内容（500字内）不能为空', trigger: 'blur' }
        ],
        rating: [
          { required: true, message: '星级评分（1-5）不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '审核状态不能为空', trigger: 'blur' }
        ],
        createdAt: [
          { required: true, message: '评价时间不能为空', trigger: 'blur' }
        ]
      },
      statusOptions: [{
        value: 0,
        label: '待审核'
      }, {
        value: 1,
        label: ' 审核通过'
      }, {
        value: 2,
        label: '审核不通过'
      }],
    }
  },
  methods: {
    init(id) {
      this.dataForm.commentId = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.commentId) {
          this.$http({
            url: this.$http.adornUrl(`/product/comment/info/${this.dataForm.commentId}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 200) {
              this.dataForm.userId = data.data.userId
              this.dataForm.productId = data.data.productId
              this.dataForm.orderId = data.data.orderId
              this.dataForm.content = data.data.content
              this.dataForm.rating = data.data.rating
              this.dataForm.status = data.data.status
              this.dataForm.createdAt = data.data.createdAt
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/product/comment/${!this.dataForm.commentId ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'commentId': this.dataForm.commentId || undefined,
              'userId': this.dataForm.userId,
              'productId': this.dataForm.productId,
              'orderId': this.dataForm.orderId,
              'content': this.dataForm.content,
              'rating': this.dataForm.rating,
              'status': this.dataForm.status,
              'createdAt': this.dataForm.createdAt
            })
          }).then(({ data }) => {
            if (data && data.code === 200) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    },

  }
}
</script>
